
/*  
  ClearCanvas (tentative name) is an HTML5 Canvas API
  designed to extend the transparancy effects of the canvas.
  
  @return => true, if ClearCanvas was successfully created
             false, otherwise

*/
var ClearCanvas = (function(front_canvas) {
  var lineWidth = 5;
  var current_ctx = front_canvas.getContext('2d');

  // Validate that we are actually given proper
  // canvas
  if (!current_ctx)
    return false;

  // Given begin and end points of a line, plot that
  // line onto canvas as a transparant brush
  // Line coordinates are computed with Bresenham's Algorithm
  function clearline(beginX, endY, beginY, endY) {
    var isSteep = Math.abs(beginY - endY) > Math.abs(beginX - endX);

    if (isSteep) {
	  // Swap beginX and beginY
      beginX = beginX ^ beginY;
	  beginY = beginX ^ beginY;
	  
	  // Swap endX and endY
	  endX = endX ^ endY;
	  endY = endX ^ endY;
	} 
	
    if (beginX > beginY) {
	  // Swap beginX and endX
	  beginX = beginX ^ endX;
	  endX   = beginX ^ endX;
	  
	  // Swap beginY and endY
	  beginY = beginY ^ endY;
	  endY   = beginY ^ endY;
	}
 
     var deltaX = endX - beginX;
     var deltaY = Math.abs(endY - beginY);
	 
	 // TODO: Handle vertical lines!
	 if (deltaX == 0)
	   return;
	 
	 var error = 0.0;
	 var deltaError = deltaY / deltaX;
	 var yStep = 1;
	 var y = beginY;
	 var x = beginX;
	 
	 if (beginY >= endY)
	   yStep = -1;
	   
	 for(;x<=endX;x++) {
       if (isSteep)
	     // plot(y,x);
	   else
	     // plot(x,y);
		 
	   error = error + deltaError;
       if ( error == 0.5) {
         y = y + yStep;
		 error = error - 1.0;
       }	 
    }  
   
  }

  function setLineWidth(newLineWidth) {
    // Verify that the requested new width is >= 1
	if (newLineWidth >= 1)
      lineWidth = newLineWidth;
  }

});